package com.mitac.mcd.cs.mobiletracking.socketserver.util;


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class testUploadClient {
	
	private static final String ADDRESS = "127.0.0.1";
	private static final int PORT = 9090;
	private static SocketAddress socketaddress = new InetSocketAddress(ADDRESS,PORT);
	public static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss yyyy/MM/dd");
	private static Log logger = LogFactory.getLog("application"); 
	
	public static Socket reconnect(Socket socket) throws IOException{
		socket.close();
		socket = null;
		socket = new Socket();
		socket.connect(socketaddress,5*1000);
		return socket;
	}
	
	public static void main(String args[]) throws IOException, InterruptedException{
		Socket socket = null;
        
        /*try {
	        while(true){
	        	
	    		socket = new Socket();
	    		socket.connect(socketaddress,5*1000);
	    		
	    		BusinessUtil.aLogger.info("Upload Client OK");
	    		Thread.sleep(100*1000);
	        }
        } catch (Exception e) {
        	socket.close();
			BusinessUtil.aLogger.info("连接初始化失败....");
			System.exit(1);
		}*/
        
        String aut = "{\"cmd\":\"aut\", \"uid\":\"7c6d6203b015\", \"dev_type\":1, \"interval\":10}";
        String data = "{\"cmd\":\"gpsupload\",\"uid\":\"7c6d6203b015\",\"coordinates\":[{\"lat\":\"30.6586020\"},{\"lon\":\"104.0648570\"},{\"height\":\"0\"}],\"speed\":10,\"date\":\"13:08:40 2010/09/07\"}";
        String d = "{\"cmd\":\"bye\", \"uid\":\"7c6d6203b015\"}";
        String e = "{\"cmd\":\"notificationcmd\", \"type\":0, \"newest\":Boolean, \"clients\":[{\"uid\":\"7c6d6203b015\", \"badge\":5 },{\"uid\":\"90840d716c43\", \"badge\":4 }]}";
        try {
        		int count = 0;
	        	boolean _run = true;
	    		socket = new Socket();
	    		socket.connect(socketaddress,5*1000);
	    		OutputStream Os = null;
				PrintStream PS = null;
				if (!socket.isClosed()){
	                Os = socket.getOutputStream();
	                PS = new PrintStream(Os);
	    		}
	    		PS.println(aut);
	    		Thread.sleep(5*1000);
	    		while(_run){
	    			PS.println(data);
	    			//BusinessUtil.aLogger.info(data);
	    			Thread.sleep(10*1000);
	    			count++;
	    			BusinessUtil.aLogger.info("count=="+count);
	    			if (count>10){
	    				break;
	    			}
	    		}
	    		//PS.println(d);
	    		Thread.sleep(50000*1000);
	    		BusinessUtil.aLogger.info("Upload Client OK");
	    		PS.close();
	            Os.close();
	            socket.close();
	    		
        } catch (Exception e1) {
        	socket.close();
			BusinessUtil.aLogger.info("连接初始化失败....");
			logger.info("连接初始化失败....");
			System.exit(1);
		}
        
	}
}
